package com.lvmama.pangolin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.common.JSONUtil;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.RuleLevelModel;
import com.lvmama.pangolin.service.RuleLevelService;
import com.lvmama.pangolin.util.HttpsUtil;
import com.lvmama.pangolin.util.HttpsUtil.HttpResponseWrapper;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/background")
public class PriceSafetyController {
	
	@Autowired
	RuleLevelService ruleLevelService;
	
	@RequestMapping("/priceSafety")
	public String priceSafety(ModelMap model,String id,String level_type,String busi_type,String rule_level,String left_value,
			String right_value,String start_date,String end_date,String is_open,String remark){
		
		RuleLevelModel rm=new RuleLevelModel();
		//查询条件
		RuleLevelModel rm_s=new RuleLevelModel();
		if(level_type==null || level_type.trim()==""){//级别类型  Normal-一般级别 Super-特殊级别
			level_type="Normal";
		}
		if(is_open!=null&&is_open.trim()!=""){
			rm.setIs_open(Integer.valueOf(is_open));
		}
		if(left_value!=null&&left_value.trim()!=""){
			rm.setLeft_value(Double.valueOf(left_value));
		}
		if(right_value!=null&&right_value.trim()!=""){
			rm.setRight_value(Double.valueOf(right_value));
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			if(end_date!=null&&end_date!=""){
				rm.setEnd_date(sdf.parse(end_date));
			}
			if(start_date!=null&&start_date!=""){
				rm.setStart_date(sdf.parse(start_date));
			}
			
		} catch (ParseException e2) {
			e2.printStackTrace();
		}
		
		if(id!=null&&id!=""){//修改
			rm.setId(Integer.valueOf(id));
			rm.setBusiness_type(busi_type);
			rm.setRemark(remark);
			rm.setRule_level(Integer.valueOf(rule_level));
			try {
				ruleLevelService.updteTimeLevel(rm);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if(id==""){//新增
			try {
				if(busi_type!=null&&busi_type.trim()!=""&&rule_level!=null&&rule_level.trim()!=""){
					if(level_type.equals("Super")&&(remark==null||remark=="")){
						model.addAttribute("remark_info","描述不能为空！");
					}else{
						rm.setBusiness_type(busi_type);
						rm.setRule_level(Integer.valueOf(rule_level));
						rm.setRemark(remark);
						rm.setLevel_type(level_type);
						ruleLevelService.insertTimeLevel(rm);
					}
				}else if(rule_level==null||rule_level.trim()==""){
					
					model.addAttribute("rule_level_info","级别不能为空！");
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
			rm_s.setBusiness_type(busi_type);
			rm_s.setLevel_type(level_type);
		
		List<RuleLevelModel> timeLevels=ruleLevelService.getTimeLevel(rm_s);
		if(level_type.equals("Normal")){
			model.addAttribute("normalLevels", timeLevels);
		}else{
			model.addAttribute("superLevels", timeLevels);
		}
		model.addAttribute("level_type", level_type);
		model.addAttribute("busi_type", busi_type);
		return "/background/priceSafety";
	}
	/**
	 * 删除特殊级别
	 * @param id
	 * @param model
	 */
	@RequestMapping("/deleteSuperLevel")
	@ResponseBody
	public void deleteSuperLevel(String id){
		try {
			int ids=Integer.valueOf(id);
			ruleLevelService.deleteLevelByID(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 局部刷新页面
	 * @param id
	 * @param rule_level
	 * @param left_value
	 * @param right_value
	 * @param start_date
	 * @param end_date
	 * @param is_open
	 * @param remark
	 */
	@RequestMapping("/updateSuperLevel")
	@ResponseBody
	public void updateSuperLevel(String id,String rule_level,String left_value,
			String right_value,String start_date,String end_date,String is_open,String remark){
		RuleLevelModel rm=new RuleLevelModel();
		if(is_open!=null&&is_open!=""){
			rm.setIs_open(Integer.valueOf(is_open));
		}
		if(left_value!=null&&left_value!=""){
			rm.setLeft_value(Double.valueOf(left_value));
		}
		if(right_value!=null&&right_value!=""){
			rm.setRight_value(Double.valueOf(right_value));
		}
		if(rule_level!=null&&rule_level!=""){
			rm.setRule_level(Integer.valueOf(rule_level));
		}
		if(remark!=null&&remark!=""){
			rm.setRemark(remark);
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		try {
			if(end_date!=null&&end_date!=""){
				rm.setEnd_date(sdf.parse(end_date));
			}
			if(start_date!=null&&start_date!=""){
				rm.setStart_date(sdf.parse(start_date));
			}
			
		} catch (ParseException e2) {
			e2.printStackTrace();
		}
		
		if(id!=null&&id!=""){
			rm.setId(Integer.valueOf(id));
			try {
				ruleLevelService.updteTimeLevel(rm);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if(id==""){
			
			try {
				rm.setLevel_type("Super");
				ruleLevelService.insertTimeLevel(rm);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 重新加载校验配置
	 * @param id
	 * @param model
	 */
	@RequestMapping("/reload/roleLevel")
	@ResponseBody
	public Map<String,String> reloadRoleLevel(){
		Map<String,String> resultMap = new HashMap<>();
		ConfigManager config = ConfigManager.getInstance();
		String url = config.getParamValue("PANGOLIN_RELEVANCE_URL", "PANGOLIN_RELEVANCE_URL")+
				config.getParamValue("PANGOLIN_RELEVANCE_HANDELER", "RELOAD_ROLE_LEVEL");
		try {
			HttpResponseWrapper rs = HttpsUtil.requestPostData(url, "", "application/json", "UTF-8");
			String str = rs.getResponseString();
			if(StringTools.isValid(str))
			{
				JSONObject jo = JSONUtil.getObject(str);
				String code = jo.getString("code");
				if("0".equals(code))
				{
					resultMap.put("Success", "同步价格校验配置数据成功!");
				}
			}
		} catch (Exception e) {
			resultMap.put("Error", "同步价格校验配置数据失败!");
		}
		return resultMap;
	}
}
